package com.huang;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author HuangShen
 * @Description 动态代理测试
 * @create 2021-03-29 14:10
 */
public class MyTest {



    @Test
    public  void  staticProxy(){
        Server server = new Server();
        ProxyServer proxyServer = new ProxyServer(server);
        proxyServer.browse();
        System.out.println();

    }


    @Test
    public void autoProxy(){
        Server server = new Server();
        NetWork proxyInstance = (NetWork) ProxyFactory.getProxyInstance(server);
        proxyInstance.browse();
    }




    @Test
    public void  lambdaTest1(){
        //消费性接口
        Consumer<String> consumer= System.out::println;
        Consumer<String> consumer1= System.out::println;
        consumer.accept("hhhh");
        consumer.andThen(consumer1).accept("hhh");

        //供给型接口
        Supplier<Integer> supplier=()-> new Random().nextInt(100);
        System.out.println(supplier.get());


        //对T类型转换为R类型
        Function<String,Integer> function= Integer::parseInt;
        Integer apply = function.apply("100");
        System.out.println(function.apply("100"));

        //确定T类型是否满足约束 返回boolean值
        Predicate<String> predicate=(s)-> s.equals("tt");
        Predicate<String> predicate2=(s)-> s.equals("t");
        System.out.println("======================");
        System.out.println(predicate2.or(predicate).test("t"));
        System.out.println(predicate.test("tt"));

    }


    @Test
    public void test1(){
        new S();
    }

    @Test
    public void  streamTest(){

        List<String> strings = Arrays.asList("aa", "ba","ca","da");
        List<String> string1 = Arrays.asList("a", "b","k","d");
        strings.stream().filter(s->!s.equals("2")).forEach(System.out::println);
        strings.stream().map(String::toUpperCase).forEach(System.out::println);
        string1.stream().sorted().forEach(System.out::print);
        boolean b = string1.stream().anyMatch(s -> s.equals(""));
        string1.stream().findFirst();


        strings.stream().flatMap((Function<String, Stream<Character>>) this::fromStringToStream).distinct().forEach(System.out::println);



    }



    @Test
    public void  streamAPITest(){
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
        System.out.println(integers.stream().count());
     integers.stream().map(i -> i + 1).collect(Collectors.toList()).forEach(System.out::print);

        System.out.println(integers.stream().reduce(0, Integer::sum));
    }


    public Stream<Character>  fromStringToStream(String s){
        ArrayList<Character> objects = new ArrayList<>();
        for (char c : s.toCharArray()) {
            objects.add(c);
        }

        return objects.stream();
    }


}



class F{

    public F() {
        System.out.println("F");
    }

    public F(String s) {
        System.out.println("F有参");
    }

}


class S extends F{

    private String s;



    public S(){
        super("s");
        System.out.println("S");
    }

    public S(String s){
        System.out.println("S");
    }
    public void hh(String ...a){

        System.out.println("hh");
    }
}